package com.ma4567.remotecontroller.UserManageRequest;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ma4567.remotecontroller.MysqlHelper.TableLogManager;
import com.ma4567.remotecontroller.MysqlHelper.TableRequestLog;
import com.ma4567.remotecontroller.RemoteControllerApplication;
import com.ma4567.remotecontroller.UserManager.User;
import com.ma4567.remotecontroller.ffmpegManager.ffmpegController;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.io.Console;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

class getUserListResponse extends com.ma4567.remotecontroller.util.CustomRespone{
    public List<User> Users;

    public getUserListResponse(int Status, List<User> Users) {// 0 正常 1 cookie错误 4 没权限
        super(Status);
        this.Users = Users;
    }
}

@CrossOrigin
@RestController
public class GetUserList {
    @RequestMapping(value = "/api/getUserList", method = RequestMethod.GET)
    public String getUserList(HttpServletRequest request,
                              @RequestParam("cookie") String Cookie) throws SQLException {
        ffmpegController.StopStream();
        TableRequestLog.AddLog("Request user list", Cookie, request.getRemoteAddr());
        if(!RemoteControllerApplication.userManager.JudgeCookie(Cookie)){//鉴cookie
            return JSONObject.toJSONString(new getUserListResponse(1, null), SerializerFeature.PrettyFormat);
        }
        else {
            if(!RemoteControllerApplication.userManager.GetUserSingleItemPermission(Cookie, "Root")){//鉴权
                return JSONObject.toJSONString(new getUserListResponse(2, null), SerializerFeature.PrettyFormat);
            }
            else {
                TableLogManager.AddLog("Request user list", Cookie);
                return JSONObject.toJSONString(new getUserListResponse(0, RemoteControllerApplication.userManager.GetAllUser()), SerializerFeature.PrettyFormat);
            }
        }
    }
}
